/**
 * 店铺申请的审核结果提交处理
 * @param {object} params 
 */
module.exports = async function(params = {}) {
	// const clientInfo = this.getUniversalClientInfo()
	const dbJQL = uniCloud.databaseForJQL({
		clientInfo: this.clientInfo
	})
	// 管理员账号
	let currentUserInfo = await this.uniIdCommon.checkToken(this.getUniIdToken())
	let formDataId = params.formDataId
	let originData = params.originData
	params.reviewer_id = currentUserInfo.uid
	// params.state=0
	delete params.formDataId
	delete params.originData
	let res1 = await dbJQL.collection('tian-mall-shop-apply').doc(formDataId).update(params)
	let res2,res3,res4
	if (params.state == 1) {
		// this.setShopManager()
		let value = originData;
		delete value._id;
		delete value.apply_result;
		let newVal = Object.assign({}, {
			...value,
			state: true,
			online: true,
			id:originData.id,
			address: {
				...value.address,
				location: new dbJQL.Geo.Point(value.address.longitude, value.address.latitude)
			},
			deliveryTypes: ["deliveryHome"],
			// apply_id: this.formDataId
		})
		// 创建tian-mall-shops表数据
		// res2 = await dbJQL.action("genId").collection("tian-mall-shops").add(newVal)
		res2 = await dbJQL.collection("tian-mall-shops").add(newVal)
		// 设置users表相关字段
		let {
			data
		} = await dbJQL.collection('uni-id-users').doc(originData.uid).field('dcloud_appid,role').get({
			getOne: true
		})
		
		let updateData={
			dcloud_appid:Array.from(new Set([...data.dcloud_appid,this.wjfCommonConfig.appIds.shopAdmin])),
			role:Array.from(new Set([...data.role,"shop_owner","shop_cust_ser"])),
			shop_id:res2.id
		}
		// 更新user表数据
		res3= await dbJQL.collection('uni-id-users').doc(originData.uid).update(updateData)
		
		let managerData={
			user_id:originData.uid,
			type:1,
			role:["shop_owner"],
			shop_id:res2.id
		}
		
		res4=await dbJQL.collection('wjf-management-persons').add(managerData)
	}


	return {
		errCode: 0,
		errMsg: 'submit-shop-application-review-success',
		params,
		res1,
		res2,
		res3,
		res4
	}
}